package com.msb.mall.interceptor;

import com.msb.common.constant.AuthConstant;
import com.msb.common.vo.MemberVO;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @Author zhangjiajun
 * @Email zhangajun@hydsoft.com
 * @Description  自定义拦截器 获取当前用户登录信息
 * @Date 2025/7/31 20:58
 */
public class AuthInterceptor implements HandlerInterceptor {

    //本地线程对象
    public static ThreadLocal<MemberVO> threadLocal = new ThreadLocal<>();

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
       //通过httpsession获取登录的用户信息
        HttpSession session = request.getSession();
        Object attribute = session.getAttribute(AuthConstant.AUTH_SESSION_REDIS);
        if (attribute != null) {
            MemberVO member = (MemberVO) attribute;
            threadLocal.set(member);
        }
        return true;
    }
}
